package com.thalesgroup.nl.trtdelft.dein.mapper.example;

import com.thalesgroup.nl.trtdelft.dein.mapper.AbstractMappedWebService;
import com.thalesgroup.nl.trtdelft.dein.servlet.impl.Client;
import java.io.IOException;
import java.io.Serializable;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.http.HttpException;
import org.json.simple.JSONObject;

/**
 *
 * @author AbdelhaqA
 */
public class YahooWeatherService extends AbstractMappedWebService {

    @Override
    @SuppressWarnings("unchecked")
    public void input(String senderAddress, String inputVariableName, Serializable input) {

    }

    @Override
    public void start() {
        JSONObject args = new JSONObject();
        String coord = (String) queryReceiveVariableContent.get("gpsWeather");
       // String coord = "139.730242,35.662454";
        try {
            String weatherInfo = Client.get("http://weather.olp.yahooapis.jp/v1/place?appid=dj0zaiZpPW04aDNSeU5tRWZLbSZzPWNvbnN1bWVyc2VjcmV0Jng9NTM-&coordinates="+coord, args.toJSONString());
            if (weatherInfo != null)
            outputs.put("weatherForecast", weatherInfo); 
       //     sendMapperInformationInterface.sendOutput();
        } catch (URISyntaxException ex) {
                Logger.getLogger(YahooWeatherService.class.getName()).log(Level.SEVERE, null, ex);
        } catch (HttpException ex) {
                Logger.getLogger(YahooWeatherService.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
                Logger.getLogger(YahooWeatherService.class.getName()).log(Level.SEVERE, null, ex);
        }                 
    }

    @Override
    public void computeOutput(String variableName) throws IllegalStateException {
        computeOutput();
    }

    @Override
    public Serializable getCurrentOutput(String variableName) throws IllegalStateException {
        return outputs.get(variableName);
    }

}
